//-----------------------------------Cloud Code--------------------------------
  
/*var stripe = require("stripe")(
  "sk_test_waGgPKE1zA91Nru33emcGIUL"
);*/
  
var stripe = require('stripe');
stripe.initialize('sk_test_waGgPKE1zA91Nru33emcGIUL');
  
  
/*--------------------------------test cloud function---------------------------
 *Function      : hello
 *Input         : None
 *Output        : String "Hello World"
 *Description   : This is a function to for testing cloud code
 */
Parse.Cloud.define("hello", function(request, response) {
    console.log(request.params);
  response.success("Hello world!");
  console.log(response.success);
});
  
  
/*---------------------------------CREATE CARD TOKEN--------------------------------
 *Function      : createCardToken()
 *Input         : 
 *Output        : 
 *Description   : This function is used to  create a token representing credit/debit 
 *              : card details 
 */
Parse.Cloud.define("createCardToken", function(params, response) {
    stripe.tokens.create({
        card: {
            "number": '4242424242424242',
            "exp_month": 12,
            "exp_year": 2015,
            "cvc": '123'
        }
    }, 
    {
        success: function(httpResponse) {
            response.success("token created");
        },
        error: function(httpResponse) {
            response.error("Oops! Something went wrong");
        }
    });
});
   
 /*---------------------------CREATE BANK ACCOUNT TOKEN----------------------------
 *Function      : createBankAccountToken()
 *Input         : 
 *Output        : 
 *Description   : This function is used to  create a token representing
 *              : bank account information 
 */
Parse.Cloud.define("createBankAccountToken", function(request, response) {
    var country = request.params.Country;
    var account = request.params.AccountNumber;
    var routing = request.params.RoutingNumber;
    stripe.tokens.create({
        bank_account: { 
            country: country,
            routing_number: routing,
            account_number: account
        }
    }, 
    {
        success: function(httpResponse) {
            response.success(httpResponse);
            console.log(httpResponse);
        },
        error: function(httpResponse) {
            console.error(httpResponse.message);
            response.error(httpResponse.message);
        }
    });
});
  
 
 
/*-------------------------------CHARGE CUSTOMER------------------------------------------
 *Function      : chargeCustomer()
 *Input         : Double amount,  String currency, String card token
 *Output        : 
 *Description   : This function is used to charge user using their customer id 
 
 */
Parse.Cloud.define("chargeCustomer", function(request, response) {
    var currency = request.params.Currency;
    var amount = request.params.Amount;
    var customerId = request.params.Customer;
    stripe.Charges.create({
        amount: amount,
        currency: currency,
        customer: customerId
    },
    {
        success: function(httpResponse) {
            response.success(httpResponse);
            console.log(httpResponse);
        },
        error: function(httpResponse) {
            //response.error(httpResponse);
            console.error(httpResponse.message);
            response.error(httpResponse.message);
        }
    });
});
  
  
/*-------------------------------CHARGE CREDIT CARD-----------------------------
 *Function      : chargeCreditCard()
 *Input         : Double amount,  String currency, String card token
 *Output        : 
 *Description   : This function is used to charge user credit card given the amount
 *              : currency, and the card token
 */
Parse.Cloud.define("chargeCreditCard", function(request, response) {
    var currency = request.params.Currency;
    var amount = request.params.Amount;
    var token = request.params.Token;
    stripe.Charges.create({
        amount: amount,
        currency: currency,
        card: token
    },
    {
        success: function(httpResponse) {
            response.success(httpResponse);
            console.log(httpResponse);
        },
        error: function(httpResponse) {
            //response.error(httpResponse);
            console.error(httpResponse.message);
            response.error(httpResponse.message);
        }
    });
});
  
  
/*---------------------------------CREATE REFUND-------------------------------
 *Function      : createRefund()
 *Input         : 
 *Output        : 
 *Description   : This function is used            
 */
Parse.Cloud.define("createRefund", function(chargeId, response) {
    stripe.Charges.createRefund(
        chargeId,
    { },
    {
        success: function(httpResponse) {
            response.success("Refund Successful");
        },
        error: function(httpResponse) {
            response.error("Oops! Something went wrong");
        }
    });
});
  
  
/*----------------------------RETRIEVE EXISTING CHARGE---------------------------
 *Function      : retrieveCharge()
 *Input         : String charge ID
 *Output        : 
 *Description   : This function is used to retrieve an existing charge             
 */
Parse.Cloud.define("retrieveCharge", function(chargeId, response) {
    stripe.Charges.retrieve(
        chargeId,
    {
        success: function(httpResponse) {
            response.success("charge Retrieved");
        },
        error: function(httpResponse) {
            response.error("Oops! Something went wrong");
        }
    });
});
  
/*----------------------------CREATE NEW CUSTOMER---------------------------
 *Function      : createCustomer()
 *Input         : 
 *Output        : 
 *Description   : This function is used to             
 */
Parse.Cloud.define("createCustomer", function(request, response) {
    var token = request.params.Token;
    stripe.customers.create({
        description: 'New Customer',
        card: token 
    },
    {
        success: function(httpResponse) {
            response.success(httpResponse);
            console.log(httpResponse);
        },
        error: function(httpResponse) {
            //response.error(httpResponse);
            console.error(httpResponse.message);
            response.error(httpResponse.message);
        }
    });
});
  
/*----------------------------RETRIEVE CUSTOMER---------------------------
 *Function      : retrieveCustomer()
 *Input         : 
 *Output        : 
 *Description   : This function is used to             
 */
Parse.Cloud.define("retrieveCustomer", function(customerId, response) {
    stripe.customers.retrieve(
    customerId,
    {
        success: function(httpResponse) {
            response.success("Customer retrieved");
        },
        error: function(httpResponse) {
            response.error("Oops! Something went wrong");
        }
    });
});
   
 /*----------------------------TRANSFER FUNDS---------------------------
 *Function      : tranferFunds()
 *Input         : 
 *Output        : 
 *Description   : This function is used to             
 */
Parse.Cloud.define("tranferFunds", function(recipientId, response) {
    stripe.transfers.create({
        amount: 400,
        currency: "usd",
        recipient: recipientId,
        description: "Transfer for test@example.com"
    }, 
    {
        success: function(httpResponse) {
            response.success("Funds transferred");
        },
        error: function(httpResponse) {
            response.error("Oops! Something went wrong");
        }
    });
});
   
   
 /*----------------------------CANCEL TRANSFER---------------------------
 *Function      : cancelTransfer()
 *Input         : 
 *Output        : 
 *Description   : This function is used to             
 */
Parse.Cloud.define("cancelTransfer", function(transferId, response) {
    stripe.transfers.cancel(
        transferId, 
    {
        success: function(httpResponse) {
            response.success("Transfer cancelled");
        },
        error: function(httpResponse) {
            response.error("Oops! Something went wrong");
        }
    });
});
  
 /*----------------------------CREATE RECIPIENT---------------------------
 *Function      : creatRecipient()
 *Input         : 
 *Output        : 
 *Description   : This function is used to             
 */
Parse.Cloud.define("createRecipient", function(accountToken, response) {
    stripe.recipients.create({
        name: "First Last",
        type: "individual/corporation",
        bank_account: accountToken
    }, 
    {
        success: function(httpResponse) {
            response.success("recipient created");
        },
        error: function(httpResponse) {
            response.error("Oops! Something went wrong");
        }
    });
});
   
 /*----------------------------RETRIEVE RECIPIENT------------------------------------------------------
 *Function      : retrievRecipient()
 *Input         : 
 *Output        : 
 *Description   : This function is used to             
 */
Parse.Cloud.define("retrieveRecipient", function(recipientId, response) {
    stripe.recipients.retrieve(
        recipientId, 
    {
        success: function(httpResponse) {
            response.success("recipient created");
        },
        error: function(httpResponse) {
            response.error("Oops! Something went wrong");
        }
    });
});